﻿// MIT License
//
// Copyright (c) 2021-present zuohuaijun, Daming Co.,Ltd and Contributors
//
// 电话/微信：18020030720 QQ群1：87333204 QQ群2：252381476

using Admin.NET.Application.Const;
using Admin.NET.Core.Service;
using Admin.Net.InformationPlatform.Entity;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Nest;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Admin.NET.Core;
using Admin.Net.InformationPlatform.Entity.Maintenance;
using Admin.Net.InformationPlatform.Service.Maintenance.Dto;
using SjEntityClass.Entity.T100;
using Furion.FriendlyException;
using SjEntityClass;

namespace Admin.Net.InformationPlatform.Service.Maintenance
{
    /// <summary>
    /// 运维平台-T100用户密码重置禁用管理
    /// </summary>
    [ApiDescriptionSettings(groups: "Maintenance", Name = "T100用户密码重置禁用管理", Tag = "T100用户密码重置禁用管理", Order = 200)]


    public class T100UserService : IDynamicApiController, ITransient
    {

        private readonly SysUserRoleService _sysUserRoleService;
        /// <summary>
        /// 单例数据链接
        /// </summary>
        ISqlSugarClient db;
        /// <summary>
        /// T100数据库
        /// </summary>
        private SqlSugarProvider _T100Db;
        private SqlSugarProvider _SjDb;
        private SqlSugarProvider _plm;
        private SqlSugarProvider _Dilon;


        /// <summary>
        /// 用户信息
        /// </summary>
        TokenUser _User;
        /// <summary>
        /// 成品仓库管理模块
        /// </summary>
        /// <param name="db"></param>
        public T100UserService(ISqlSugarClient db, SysUserRoleService sysUserRoleService)
        {
            this.db = db;
            this._T100Db = this.db.AsTenant().GetConnection(T100TestArea.ConfigId);
            this._SjDb = this.db.AsTenant().GetConnection(SJDB.ConfigId);
            this._plm = this.db.AsTenant().GetConnection(PLM.ConfigId);
            this._Dilon = this.db.AsTenant().GetConnection(Dilon.ConfigId);
            // 获取 `Jwt` 存储的信息 企业编号与据点信息
            _User = Util.SjCommonUtil.GetT100UserTokenInfo(_T100Db, _Dilon);
            _sysUserRoleService = sysUserRoleService;

        }
        /// <summary>
        ///  获取T100用户列表-分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("/YWPT/GetT100UserPagedList")]
        public async Task<SqlSugarPagedList<T100UserOutPut>> GetT100UserPagedList(T100UserInput input)
        {
            var output = await _T100Db.Queryable<GzxaT>()
                  .LeftJoin<OoagT>((l,o) => (l.GZXA001 == o.Ooag001))
.WhereIF(!string.IsNullOrEmpty(input.UserCode), (l) => (l.GZXA001.Contains(input.UserCode)))//用户工号
.WhereIF(!string.IsNullOrEmpty(input.UserName), (l,o) => (o.Ooag011.Contains(input.UserName)))//用户姓名

.OrderBy(l => l.GZXA001, OrderByType.Desc) //用户工号排序
.Select((l,o) => new T100UserOutPut
{
   USERCODE = l.GZXA001,
   USERNAME = o.Ooag011,
   USERSTATUS = l.GZXASTUS

}).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }

        /// <summary>
        /// 用户停用启用更改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/YWPT/UpdateUserState")]
        public async Task<string> UpdateMouldList(T100UserState input)
        {
            if ((new string[] { "Y","N" }).Contains(input.UserState))
            {
                //判断单据存在
                int cnt = await _T100Db.Queryable<GzxaT>()
                    .Where(a => a.GZXA001 == input.UserCode)
                    .Select(a => a.GZXA001)
                    .CountAsync();
                if (cnt == 0)
                {
                    throw Oops.Oh("任务不存在，驳回！");
                }
                else
                {
                    //提交
                    try
                    {
                        _SjDb.Ado.BeginTran();
                        int count = await _T100Db.Updateable<GzxaT>()
                            .SetColumns(a => a.GZXASTUS == input.UserState)
                            .Where(a => a.GZXA001 == input.UserCode)
                            .ExecuteCommandAsync();

                        if (count != 1)
                        {
                            throw Oops.Oh("修改失败！");
                        }
                        _SjDb.Ado.CommitTran();
                        return "修改成功！";
                    }
                    catch (Exception e)
                    {
                        _SjDb.Ado.RollbackTran();
                        throw Oops.Oh($"异常：{e.Message}");
                    }
                }
            }
            else
            {
                throw Oops.Oh("状态码提交异常，驳回！");
            }

        }
    }
}